<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-form-item label="用户名" prop="username">
				<el-input v-model="dataForm.username" placeholder="用户名"></el-input>
			</el-form-item>
			<el-form-item label="昵称" prop="nickname">
				<el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
			</el-form-item>
			<el-form-item label="密码" prop="password">
				<el-input v-model="dataForm.password" placeholder="密码"></el-input>
			</el-form-item>
			<el-form-item label="手机号" prop="phone">
				<el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
			</el-form-item>
			<el-form-item label="头像" prop="avatar">
				<el-image v-if="this.dataForm.avatar" style="width: 100px; height: 100px" :src="this.dataForm.avatar"
					:fit="fit"></el-image>
				<el-upload class="avatar-uploader" action="http://localhost:9237/cos/uploadCosFile"
					:show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
					<i v-if="!this.dataForm.avatar" class="el-icon-plus avatar-uploader-icon"></i>
				</el-upload>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				dataForm: {
					id: 0,
					username: '',
					nickname: '',
					password: '',
					phone: '',
					avatar: ''
				},
				dataRule: {
					username: [{
						required: true,
						message: '用户名（手机号）不能为空',
						trigger: 'blur'
					}],
					nickname: [{
						required: true,
						message: '昵称不能为空',
						trigger: 'blur'
					}],
					password: [{
						required: true,
						message: '密码不能为空',
						trigger: 'blur'
					}],
					phone: [{
						required: true,
						message: '手机号不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.visible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
					if (this.dataForm.id) {
						this.$http.post('/user/searchUserInfo', {
							userId: id
						}).then(resp => {
							if (resp.data.code === 200) {
								let result = resp.data;
								this.dataForm = result
							}
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						if (!this.dataForm.id) {
							this.$http.post('/user/register', this.dataForm).then(resp => {
								if (resp.data.code === 200) {
									this.$message({
										message: '新增成功',
										type: 'success',
										duration: 1200,
									})
									this.visible = false;
									this.$emit('refreshDataList')
								}
							})
						} else {
							let user = {
								id: this.dataForm.id,
								username: this.dataForm.username,
								nickname: this.dataForm.nickname,
								password: this.dataForm.password,
								avatar: this.dataForm.avatar
							}
							let data = user
							this.$http.post('/user/update', data).then(resp => {
								if (resp.data.code === 200) {
									this.$message({
										message: '更新成功',
										type: 'success',
										duration: 1200
									})
									this.visible = false;
									this.$emit('refreshDataList')
								}
							})
						}
					}
				})
			},
			beforeAvatarUpload(files) {
				const formData = new FormData();
				formData.append('files', files);
				formData.append('type', 'avatar')
				for (const [key, value] of formData.entries()) {
					console.log(key, value);
				}

				// 设置请求头
				const config = {
					headers: {
						'Content-Type': 'multipart/form-data'
					}
				};

				this.$http.post('/cos/uploadCosFile', formData, config).then(resp => {
					this.dataForm.avatar = resp.data.urls[0]
					this.$message({
						message: '上传成功',
						type: 'success',
						duration: 1200
					})
				})
				return false
			}
		}
	}
</script>